home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / KPlib 1.2.1 / sample_progs / spellcheck.cxx < prev   
Encoding:
Text File  |  1994-11-07  |  1.5 KB  |  51 lines  |  [TEXT/R*ch]

  1. // Written by Keith Pomakis during the summer of 1994.
  2. // Released to the public domain on October 10, 1994.
  3.  
  4. // This is just a sample program to illustrate how easy it is to write
  5. // complex programs with my class library.  The following program accepts
  6. // input through stdin, and prints out all inputted words that do not exist
  7. // in the on-line dictionary (specified by WORD_FILE).
  8.  
  9. #include <iostream.h>
  10. #include <fstream.h>
  11. #include <ctype.h>
  12. #include "../KPbasic.h"
  13. #include "../KPList.h"
  14. #include "../KPSet.h"
  15. #include "../KPString.h"
  16.  
  17. #define WORD_FILE   "/usr/dict/words"
  18. #define SEPARATORS  " \t\n,.<>:;\042!@#$%^&*()-=+[]{}\\|`~?"
  19.  
  20. bool is_valid(const KPString &s)
  21. { return isalpha(s[0]); }
  22.  
  23. int
  24. main()
  25. {
  26.     KPSet<KPString> dictset, wordset;
  27.     KPString word;
  28.  
  29.     // Since the dictionary is sorted, reading it into a set is very quick.
  30.     cout << "Reading dictionary..." << flush;
  31.     ifstream dictstream(WORD_FILE, ios::in);
  32.     while (word.read_token(dictstream) != "")
  33.         dictset += word.to_lower();
  34.     dictstream.close();
  35.     cout << " done!\n";
  36.  
  37.     while (word.read_token(cin, SEPARATORS) != "")
  38.         wordset += word.to_lower();
  39.  
  40.     wordset = wordset.all_such_that(is_valid);
  41.     cout << "Number of unique words in document: " << wordset.size() << '\n';
  42.  
  43.     cout << "Words not in dictionary:\n";
  44.     KPList<KPString> misspelt = wordset - dictset;
  45.     KPReadOnlyIterator<KPString> iter(misspelt);
  46.     FOREACH(iter)
  47.         cout << "    " << *iter << '\n';
  48.  
  49.     return true;
  50. }
  51.